<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\News;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class NewsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new News());
        $grid->model()->latest();
        $grid->column('id', __('Id'))->sortable();
        $grid->column('title', __('文章标题'))->width(360);
        $grid->column('category_id', __('分类id'));
        $grid->column('category_name',__('分类名称'))->display(function () {
            $res = Category::find($this->category_id);
            if ($res){
                $s = $res->name;
            }else{
                $s = '';
            }
            return $s;
        });
        $grid->column('image', __('文章图片'));
        //$grid->column('content', __('Content'));
        $grid->column('all_num', __('实际累计金额'));
        $grid->column('pro_num', __('预计金额'));
        $grid->column('one_num', __('单次贡献金额'));
        $grid->column('is_recomend', __('推荐权重排序'));
        $grid->column('created_at', __('创建时间'));
       /* $grid->column('updated_at', __('修改时间'));*/

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(News::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('文章标题'));
        $show->field('image', __('文章图片'));
        $show->field('content', __('内容'));
        $show->field('all_num', __('实际累计金额'));
        $show->field('pro_num', __('预计金额'));
        $show->field('one_num', __('单次贡献金额'));
        $show->field('is_recomend', __('推荐权重排序'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('修改时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new News());

        $form->text('title', __('文章标题'));
        $lists = DB::table('category')->get();
        $lists =  objectToArray($lists);
        $directors = [];
        foreach ($lists as $k=>$v){
            $directors[$v['id']]=$v['name'];
        }
        $form->select('category_id', '分类')->options($directors);
        $form->image('image', __('文章图片'));
        $form->editor('content', __('内容'));
        $form->text('all_num', __('实际累计金额'));
        $form->text('pro_num', __('预计金额'));
        $form->text('one_num', __('单次贡献金额'));
        $form->number('is_recomend', __('推荐权重排序'));

        return $form;
    }
}
